﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using UnityEngine;



public  class FileUtil
 {
    /// <summary>
    /// 文件是否存在
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public  static bool IsExist(string path)
    { 
     FileInfo file=new FileInfo(path);
        return file.Exists;
    }
    /// <summary>
    /// 写入文件
    /// </summary>
    /// <param name="path"></param>
    /// <param name="data"></param>
    public static void WriteFile(string path, byte[ ] data)
    {
        path=PathUtil.GetStandardPath(path);
        string dir = path.Substring(0,path.LastIndexOf('/'));//开始到最后一个斜杠,文件夹的路径
        if(!Directory.Exists(dir) ) { Directory.CreateDirectory(dir); }
        FileInfo file = new FileInfo(path);
        if (file.Exists)
        {
            file.Delete();
        }
        try
        {
            //以创建的形式打开文件
            using(FileStream fs=new FileStream(path,FileMode.Create,FileAccess.Write))
            {
                fs.Write(data, 0, data.Length);
               // fs.Close(); using确保了会及时释放，不需要
            }
            // fs.dispose(); using确保了会及时调用，不需要
        }
        catch (IOException ex) { Debug.LogError(ex.Message); }
    }
  }

